package org.zhang.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.zhang.shortlink.project.dao.entity.LinkDeviceStatsDO;
import org.zhang.shortlink.project.dto.req.ShortLinkStatsReqDTO;

import java.util.List;

/**
 * @author zhang
 * @description 针对表【t_link_device_stats】的数据库操作Mapper
 * @createDate 2024-04-25 22:26:14
 * @Entity org.zhang.shortlink.project.dao.entity.LinkDeviceStatsDO
 */
@Mapper
public interface LinkDeviceStatsMapper extends BaseMapper<LinkDeviceStatsDO> {
    /**
     * 记录访问设备监控数据
     */
    @Insert("INSERT INTO t_link_device_stats (id,full_short_url, date, cnt, device, create_time, update_time, del_flag) " +
            "VALUES(#{id},#{fullShortUrl}, #{date}, #{cnt}, #{device}, #{createTime},#{updateTime},#{delFlag}) " +
            "ON DUPLICATE KEY UPDATE cnt = cnt +  #{cnt};")
    void linkDeviceStats(LinkDeviceStatsDO linkDeviceStatsDO);

    /**
     * 根据短链接获取指定日期内访问设备监控数据
     */
    @Select("SELECT " +
            "    device, " +
            "    SUM(cnt) AS cnt " +
            "FROM " +
            "    t_link_device_stats " +
            "WHERE " +
            "    full_short_url = #{param.fullShortUrl} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    full_short_url, device;")
    List<LinkDeviceStatsDO> listDeviceStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

}




